import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
// 数据表名称 离线金额流水
export const offLineDtoList = 'offLineDtoList'
export const database = uni.getStorageSync('database')
/*
tradeCode 流水Id TR+年月日+六位随机数
offlineAmountOpe 当此离线金额
onlineAmountAll 在线余额
offlineAmountAll 离线余额
opeCode 操作码
productId 设备PIddle
createDate
createTime


操作码{
	add_online_auto 自动在线充值
	add_online_hand 手动在线充值
	self_income 个人收入
	punish_deduct 处罚扣款
	agency_commission 代理提成
	lease_deduct 租赁扣除
	add_offline 离线充值
}

*/
export const crateMoney = async () => {
	let sqltxt = [
		`create table if not exists ${offLineDtoList}
		("tradeCode" VARCHAR(24),
		"offlineAmountOpe" int(8),
		"onlineAmountAll" int(8),
		"offlineAmountAll" int(8),
		"opeCode" VARCHAR(16),
		"productId" VARCHAR(16),
		"createTime" VARCHAR(24),
		"createDate" VARCHAR(24))`
	];
	return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	
}


export const addOrUpdateOffMoney = async (pushData) => {

	// 数据库中tradeCode 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${offLineDtoList} where tradeCode = '${pushData.tradeCode}'`;
	let res =  await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')
		let data = setObjSql(pushData)
		let sqltxt = `INSERT  INTO ${offLineDtoList} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改
		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${offLineDtoList} set ${data} where tradeCode = '${pushData.tradeCode}'`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
		
	}
}


export const getOffMoney = async (tradeCode) => {
	let sqltxt = `select * from ${offLineDtoList}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}

export const delOffMoney = async () => {
	let sqltxt = `delete  from ${offLineDtoList}`;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}